package com.platform.modules.mall.activityV2.rule;

import com.platform.common.exception.BusinessException;
import com.platform.modules.mall.activityV2.entity.ParticipantBo;
import com.platform.modules.mall.activityV2.entity.WinnerBo;
import com.platform.modules.mall.entity.MallGoodsEntity;
import com.platform.modules.mall.entity.MallGoodsToActivityEntity;
import com.platform.modules.mall.service.MallGoodsService;
import com.platform.modules.mall.service.MallGoodsToActivityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;
import java.util.Random;


@Slf4j
@Component("drawGoodsRule")
public class DrawGoodsRule implements RuleInterface{

    @Autowired
    private MallGoodsService mallGoodsService;

    @Autowired
    private MallGoodsToActivityService mallGoodsToActivityService;
    /**
     * 抽奖中商品，没有中任何商品就抛出没有中商品
     *    * 1.抽取商品
     *          * 2.减扣商品库存
     *          *  2.1 重新抽取商品
     *          * 3.验证是否有商品
     *          * 4.创建返回的对象
     * @param bo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public WinnerBo execute(ParticipantBo bo) {
        List<MallGoodsToActivityEntity> list = bo.getList();
        /*没有商品可以抽了，直接返回null*/
        if (CollectionUtils.isEmpty(list)){
            throw new BusinessException("活动商品中没有符合要求的商品");
        }
        Integer count = count(list);
        int num=list.size();
        MallGoodsEntity goods=null;
        for (int i=0;i<num;i++){
            MallGoodsToActivityEntity entity = getEntity(list, count);

            count-=entity.getProbability().intValue();

            /*减扣活动商品库存*/
            boolean enbool = mallGoodsToActivityService.deductionNumber(entity.getId(),bo.getRequestNum());
            if (!enbool){
               // count=delEntity(entity,list,count);
                continue;
            }
            goods = mallGoodsService.shoppingNotException(entity.getGoodsId(),bo.getRequestNum(), null);
            if (goods==null){
                log.info("抽中商品id={}，商品不存在,删除该类型，重新抽取别的商品",entity.getGoodsId());
               // count=delEntity(entity,list,count);
                continue;
            }
        }
        if (goods==null){
            throw new BusinessException("没有抽中任何的商品");
        }

        WinnerBo winnerBo =new WinnerBo();
        winnerBo.setGoodsEntity(goods);
        winnerBo.setNum(bo.getNum()-bo.getRequestNum());
        winnerBo.setList(list);
        return winnerBo;
    }

    private MallGoodsToActivityEntity getEntity(List<MallGoodsToActivityEntity> list,Integer countNum){
        /*生成随机数*/
        int num = new Random().nextInt(countNum)+1;
        BigDecimal count=new BigDecimal("0");
        BigDecimal ling = new BigDecimal("0");

        Iterator<MallGoodsToActivityEntity> iterator = list.iterator();
        while (iterator.hasNext()){
            MallGoodsToActivityEntity entity = iterator.next();
            /*中奖概率为0不参与抽奖*/
            if (ling.compareTo(entity.getProbability())!=-1){
                continue;
            }
            /*计算该商品的中奖范围*/
            count=count.add(entity.getProbability());
            /*查看随机数值是否在中奖范围内，在范围内说明中奖*/
            if (num<=count.intValue()){
                iterator.remove();
                return entity;
            }
        }

        throw new BusinessException("没有抽中任何的商品");

//        for (MallGoodsToActivityEntity entity:list) {
//            /*中奖概率为0不参与抽奖*/
//            if (ling.compareTo(entity.getProbability())!=-1){
//                continue;
//            }
//            /*计算该商品的中奖范围*/
//            count=count.add(entity.getProbability());
//            /*查看随机数值是否在中奖范围内，在范围内说明中奖*/
//            if (num<=count.intValue()){
//                return entity;
//            }
//        }
        //        return null;
    }

//    private Integer delEntity(MallGoodsToActivityEntity entity,List<MallGoodsToActivityEntity> list,Integer count){
//        Iterator<MallGoodsToActivityEntity> iterator = list.iterator();
//        while (iterator.hasNext()){
//            MallGoodsToActivityEntity goods = iterator.next();
//            if (goods.getId().equals(entity.getId())){
//                count-=goods.getProbability().intValue();
//                iterator.remove();
//            }
//        }
//        return count;
//    }

    private Integer count(List<MallGoodsToActivityEntity> list){
        BigDecimal countNum =new BigDecimal("0");
        for (MallGoodsToActivityEntity entity : list) {
            countNum= countNum.add(entity.getProbability() );
        }
        return countNum.intValue();
    }
}
